package com.qiyun.duijiang.service.imp;

import com.qiyun.duijiang.dao.*;
import com.qiyun.duijiang.entity.*;
import com.qiyun.duijiang.service.ZcrjprizeService;
import com.qiyun.duijiang.utils.TimeUtil;
import com.qiyun.duijiang.utils.Zcutil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 足彩任选九开奖
 */
@Service
public class ZcrjprizeServiceImp implements ZcrjprizeService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private ZcResultMapper zcResultMapper;
    @Resource
    private ZcorderMapper zcorderMapper;
    @Resource
    private ZcwinauthMapper zcwinauthMapper;
    @Resource
    private RecordMapper recordMapper;
    @Override
    public void draw() {
        List<ZcResult> zcResults = zcResultMapper.selectByType("rj");
        ZcResult zcResult = zcResults.get(0);
        Integer issue = zcResult.getIssue();
        List<Zcorder> zcorders = zcorderMapper.selectIT(issue,"rj");
        for (Zcorder zcorder:zcorders){
            Double authup = getPoints(zcorder,zcResult);
            win(authup,zcorder);
        }
    }
    /**
     * 用户加点，修改订单状态
     * @param authup
     * @param zcorder
     */
    @Transactional
    protected void win(Double authup, Zcorder zcorder){
        if(StringUtils.isEmpty(authup)){
            zcorder.setStatus(3);
        }else{
            User user = new User();
            user.setUsername(zcorder.getUsername());
            user.setAuth(authup);
            if(authup>0) {
                Record record = new Record();
                record.setCdate(TimeUtil.dayX());
                record.setWay(4);
                record.setAuth(authup);
                record.setUsername(zcorder.getUsername());
                recordMapper.insert(record);
            }
            userMapper.updateA(user);
            zcorder.setStatus(1);
            zcorder.setWinauth(authup);
        }
        zcorderMapper.updateS(zcorder);
    }
    /**
     * 计算方案中奖点数
     * @param zcorder
     * @return
     */
    public Double getPoints(Zcorder zcorder,ZcResult zcResult){
        String rrr[] = zcResult.getResult().split(",");
        String orr[] = zcorder.getResult().split(",");
        int tem = 0;
        for (int i = 0; i<rrr.length; i++) {
            if(orr[i].contains(rrr[i])){
                tem = tem + 1;
            }
        }
        if(tem>=9){
            int winnum = Zcutil.C(tem,9);
            Zcwinauth zcwinauth = zcwinauthMapper.selectByT("rj");
            return zcwinauth.getFirstprize()*zcorder.getNum()*winnum;
        }
        return 0.00;
    }
}
